"use strict";
const path = require("path");
const BundleAnalyzerPlugin =
  require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const SelfConfig = require(path.resolve(__dirname, "./src/config"));
function resolve(dir) {
  return path.join(__dirname, dir);
}

const name = "云聘小程序";

module.exports = {
  lintOnSave: process.env.NODE_ENV === "development",
  // 路径别名
  configureWebpack: (config) => {
    let obj = {
      plugins: [new BundleAnalyzerPlugin()],
      name: name,
      resolve: {
        alias: {
          "@": resolve("src"),
        },
      },
    };
    if (process.env.NODE_ENV === "production") {
      obj = Object.assign(obj, {
        plugins: [
          new UglifyJsPlugin({
            uglifyOptions: {
              compress: {
                drop_console: true,
                drop_debugger: true,
                pure_funcs: ["console.log"], // 移除console
              },
              output: {
                comments: false,
              },
              warnings: false,
            },
          }),
        ],
      });
    }
    return obj;
  },
  chainWebpack: (config) => {
    // 排除moment.js依赖，减小打包体积
    config.externals({
      'moment': 'moment'
    });
    
    const oneOfsMap = config.module.rule("scss").oneOfs.store;
    // console.log(oneOfsMap)
    const FileName = 
      process.env.UNI_PLATFORM === "h5" ? "h5" : SelfConfig.theme;
    oneOfsMap.forEach((item) => {
      item
        .use("sass-resources-loader")
        .loader("sass-resources-loader")
        .options({
          resources: [`./src/theme/${FileName}.scss`],
        })
        .end();
    });
  },
};
